package com.htzg.jardemo.config;



import com.htzg.jardemo.properties.SwaggerProperties;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.schema.ScalarType;
import springfox.documentation.service.Contact;
import springfox.documentation.service.ParameterType;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import static java.util.Collections.singletonList;

@Data

@NoArgsConstructor
@AllArgsConstructor
@Slf4j

@Component


//@EnableSwagger2 //swagger3版本不需要使用这个注解，当然写上也无所谓~、
@EnableOpenApi
@Configuration
public class SwaggerConfig {
    @Autowired
     private SwaggerProperties swaggerProperties;


    @Bean
    public Docket docket() {
        return new Docket(DocumentationType.OAS_30)
                .pathMapping(swaggerProperties.getApplicationContextPath())

                // 定义是否开启swagger，false为关闭，可以通过变量控制，线上关闭
                .enable(swaggerProperties.getEnable())

                //配置api文档元信息
                .apiInfo(new ApiInfoBuilder()
                        .title(swaggerProperties.getApplicationName())
                        .description(swaggerProperties.getApplicationDescription())
                        .contact(new Contact("XX项目", "http://xxx.com", "110120119@qq.com"))
                        .version(swaggerProperties.getApplicationVersion())
                        .build())

                // 选择哪些接口作为swagger的doc发布
                .select()

                //apis() 控制哪些接口暴露给swagger，
                // RequestHandlerSelectors.any() 所有都暴露
                // RequestHandlerSelectors.basePackage("net.xdclass.*")  指定包位置
                // withMethodAnnotation(ApiOperation.class)标记有这个注解 ApiOperation
                .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class))

                .paths(PathSelectors.any())

                .build()
                .globalRequestParameters(
                        singletonList(new springfox.documentation.builders.RequestParameterBuilder()
                                // 不能叫Authorization
                                .name("token")
                                .description("token")
                                .in(ParameterType.HEADER)
                                .required(true)
                                .query(q -> q.model(m -> m.scalarModel(ScalarType.STRING)))
                                .build()));
    }



}
